﻿namespace JobButenko.Controllers
{
    using System.Web.Mvc;

    using JobButenko.Models;
    using JobButenko.Models.BusinessLogicLayer;

    public class ResumeController : Controller
    {
        // GET: /Resume/
        private readonly DataManager dataManager;

        public ResumeController(DataManager dataManager)
        {
            this.dataManager = dataManager;
        }

        public ActionResult Search()
        {
            this.GetData();
            return View();
        }

        [HttpPost]
        public ActionResult Search(Resume resume)
        {
            ViewData.Model = this.dataManager.Resume.SearchResume(resume);
            return View("ViewSearchResume");
        }

        public ActionResult CreateResume()
        {
            this.GetData();
            return View();
        }

        [HttpPost]
        public ActionResult CreateResume(Resume resume)
        {
            this.dataManager.Resume.CreateResume(resume);
            return RedirectToAction("ViewResume");
        }

        public ActionResult ViewResume()
        {
            ViewData["Candidate"] = this.dataManager.Candidate.GetCandidateFromUserName(User.Identity.Name);
            ViewData.Model = this.dataManager.Resume.GetResumeFromUser(User.Identity.Name);
            return View();
        }


        private void GetData()
        {
            ViewData["ProfField"] = new SelectList(
                this.dataManager.ProfessionalField.GetProfField(), "ProfFieldId", "ProfFieldName");
            ViewData["Post"] = new SelectList(this.dataManager.Post.GetPost(), "PostId", "PostName");
            ViewData["Salary"] = new SelectList(this.dataManager.Salary.GetSalary(), "SalaryId", "SalaryVol");
        }

        private void GetData(
            int desProfFieldId, int desPostId, int salaryId, int? currPostId = 1, int? currProfFieldId = 1)
        {
            ViewData["ProfField"] = new SelectList(
                this.dataManager.ProfessionalField.GetProfField(), "ProfFieldId", "ProfFieldName", currProfFieldId);
            ViewData["Post"] = new SelectList(this.dataManager.Post.GetPost(), "PostId", "PostName", currPostId);
            ViewData["DesProfField"] = new SelectList(
                this.dataManager.ProfessionalField.GetProfField(), "ProfFieldId", "ProfFieldName", desProfFieldId);
            ViewData["DesPost"] = new SelectList(this.dataManager.Post.GetPost(), "PostId", "PostName", desPostId);
            ViewData["Salar"] = new SelectList(this.dataManager.Salary.GetSalary(), "SalaryId", "SalaryVol", salaryId);
        }

        public ActionResult EditResume(int id)
        {
            var resume = this.dataManager.Resume.GetResumeFromId(id);
            ViewData.Model = resume;
            GetData(resume.DesProfFieldId, resume.DesPostId, resume.SalaryId, resume.CurrPostId, resume.CurrProfFieldId);
            return View();
        }

        [HttpPost]
        public ActionResult EditResume(int id, Resume resume)
        {
            this.dataManager.Resume.UpdateResume(id, resume);
            return RedirectToAction("ViewResume");
        }

        public ActionResult DeleteResume(int id)
        {
            this.dataManager.Resume.DeleteResume(id);
            return RedirectToAction("ViewResume");
        }
    }
}